/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package checkers3d.logic;

import checkers3d.presentation.*;
import checkers3d.storage.DataManagerAccounts;
import checkers3d.storage.DataManagerStats;
import java.awt.*;
import java.io.IOException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author sean keel
 */

public class MenuNewPlayerLogin extends GUIContainer{
    private final int CONTROL_BUTTON_AXIS = 500;
  
    GUITextbox    textboxUsername;
    GUITextbox    textboxPassword;
    GUIButton     buttonValidate;
    GUILabel      loginValidation;
    GUIButton     buttonReturn;

    public MenuNewPlayerLogin(Point size)
    {
        super(size);

        LinkedList<IDrawable> buttons = new LinkedList<IDrawable>();

        //add background image.
        add(new GUIPictureBox("Background.png", new Point()));
        buttonValidate = new GUIButton("button-createuser.png", null, new ObserverCreate());
        buttons.add( buttonValidate);

        //create the return button with behavior.
        buttonReturn = new GUIButton("button-return.png", null, new ObserverReturn());
        buttons.add(buttonReturn);
        add(new GUILabel("Create Account:", Color.WHITE, new Point(124, 230)));
        add(new GUILabel("Name:", Color.WHITE, new Point(124, 250)));
        add(new GUILabel("Password:", Color.WHITE, new Point(102, 270)));
        loginValidation = new GUILabel("   Ready", Color.RED, new Point(207, 230));
        add(loginValidation);

        textboxUsername = new GUITextbox(new Point(166, 250), 25, this);
        textboxPassword = new GUITextbox(new Point(166, 270), 25, this);
        textboxPassword.setSecure(true);
        for(IDrawable component : buttons)
            add(component);

        //compute the positions of the buttons
        UtilGUI.computeLayoutSpanning(getSize(), CONTROL_BUTTON_AXIS, buttons);}
    public void validatePlayers()
    {
        try
        {
            String username = textboxUsername.getText();
            String password = textboxPassword.getText();

            if(!DataManagerAccounts.doesPlayerExist(username)
               && !username.equals("") && !username.isEmpty()
               && !password.equals("") && !password.isEmpty()
               && !password.equals(username)){
                    loginValidation.setColor(Color.GREEN);
            }
            else
                loginValidation.setColor(Color.RED);
        }
        catch(Exception error)
        {
            loginValidation.setColor(Color.RED);
        }
    }

    @Override
    public void onKeyPress(char key)
    {
        super.onKeyPress(key);

        validatePlayers();

        if(loginValidation.getColor() == Color.GREEN)
        {
            buttonValidate.setObserver(new ObserverCreate());
        }
        else
        {
            buttonValidate.setObserver(new ObserverNull());
        }
    }
  

    private class ObserverReturn implements IInputObserver
    {
        /**
         * Returns the user to the scenario select screen.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            GameMaster.getInstance().removeTopGUIContainer();
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverCreate implements IInputObserver
    {
        /**
         * Returns the user to the scenario select screen.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            Player newPlayer = new Player(textboxUsername.getText(),
                                          textboxPassword.getText());

            try {
                DataManagerAccounts.addPlayer(newPlayer);
                //handled in addPlayer().
                //DataManagerStats.createIndividualStatistic(newPlayer);

            } catch (IOException ex) {
                JOptionPane.showMessageDialog(null, "The player could not be created.\n" +
                        "Please try again.");
                Logger.getLogger(MenuNewPlayerLogin.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            //do nothing.
        }
    }

    private class ObserverNull implements IInputObserver
    {
        /**
         * Returns the user to the scenario select screen.
         *
         * @param position Location of click within button.
         */
        public void onClick(Point position)
        {
            // Do nothing.
        }

         /**
         * Key press observer that does nothing.
         *
         * @param key Key that was pressed.
         */
        public void onKeyPress(char key)
        {
            // Do nothing.
        }
    }

}


